/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.log.controller.LogController.java 
 * <p>
 * 类产生时间: 2014-4-25 下午 13:49
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.log.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oceansoft.mobile.econsole.modules.api.service.IAppLogService;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.log.entity.AccessLog;
import com.oceansoft.mobile.econsole.modules.log.entity.SysOperLog;
import com.oceansoft.mobile.econsole.modules.log.service.IAccessLogService;
import com.oceansoft.mobile.econsole.modules.log.service.IMessageLogService;
import com.oceansoft.mobile.econsole.modules.log.service.IOperLogService;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;

/**
 * 日志处理控制器
 *
 * @author: chenw
 * @time: 2014-4-25 下午 13:49
 */
@Controller
@RequestMapping("log")
public class LogController extends BaseController {
    @Resource
    private IAppLogService appLogService;
    @Resource
    private IOperLogService logService;
    @Resource
    private IMessageLogService messageLogService;
    @Resource
    private IAccessLogService accessLogService;

    @ResponseBody
    @RequestMapping("record")
    public Result recordOperLog() {
        System.out.println("Log----------");
        return new Result();
    }

    @ResponseBody
    @RequestMapping(value = "{log_id}", method = GET)
    public Result loadLogById(@PathVariable("log_id") long logId) {
        SysOperLog sysOperLog = logService.findById(logId);
        return new Result(Result.SUCC, sysOperLog);
    }

    /**
     * 下载量统计
     *
     * @param model
     * @param query
     * @return
     */
    @RequestMapping("accesslog")
    public String accesslog(Model model, Query query) {
        List<AccessLog> list = accessLogService.loadAccessCount(query);
        model.addAttribute("list", list);
        model.addAttribute("query", query);
        return "log/accesslog-list";
    }

    /**
     * 统计量导出
     *
     * @return
     */
    @RequestMapping("exportExcel.xls")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Query query) {
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("应用统计");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short)0);  
        cell.setCellValue("应用名称");  
        cell.setCellStyle(style);  
        cell = row.createCell((short)1);  
        cell.setCellValue("使用量");  
        cell.setCellStyle(style);  
        cell = row.createCell((short)2);  
        cell.setCellValue("下载量");  
        cell.setCellStyle(style);  
  
        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
        List<AccessLog> list = accessLogService.loadAccessCount(query); 
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);  
            AccessLog log = list.get(i);  
            // 第四步，创建单元格，并设置值  
            row.createCell((short)0).setCellValue(log.getAppName());  
            row.createCell((short)1).setCellValue(log.getUseCount());  
            row.createCell((short)2).setCellValue(log.getDownCount());  
        }  
        // 第六步，将文件存到指定位置  
        try  
        {
        	response.setContentType("application/ms-excel");
        	OutputStream out = response.getOutputStream();
        	wb.write(out);  
            out.close();  
        }
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }

    @RequestMapping("clientloglist")
    public String v(Model model, Query query) {
        Pager pager = appLogService.getAppLogPage(query);
        model.addAttribute("pager", pager);
        model.addAttribute("query", query);
        log(String.format("查看客户端日志第%d页", query.getPageNum()));
        return "log/clientlog-list";
    }

    @RequestMapping(value = "viewMsgContent/{msgId}", method = GET)
    public String viewMsgContent(@PathVariable("msgId") int msgId, Model model) {
        PushMessage msg = messageLogService.findById(msgId);
        model.addAttribute("msg", msg);
        log(String.format("查看客户端日志内容"));
        return "log/clientlog-viewmsgcontent";
    }

    @RequestMapping("messageloglist")
    public String loadMessageLogByPage(Model model, Query query) {
        Pager pager = messageLogService.getMessageLogPager(query);
        model.addAttribute("pager", pager);
        model.addAttribute("query", query);
        log(String.format("查看消息第%d页", query.getPageNum()));
        return "log/messagelog-list";
    }
}